﻿Partial Public Class Wfrm_RecepcionaOC
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            ViewState("id_empresa") = Session("empresa")
            carga_grilla()
        End If
    End Sub

    Sub carga_grilla()
        Dim exec As New DotNetDal.SQLPersistence(False, "cnn", DotNetDal.SQLPersistence.Provider.SQLCLIENT)
        Dim rp As New DotNetResponse.SQLPersistence

        rp.StoredProcedure = "SP_OC_RECEPCIONA_SOLICITUD"
        rp.AddParametro(ViewState("id_empresa"))
        exec.ExecuteDataTable(rp)

        If rp.TieneDatos Then
            DataGrid2.DataSource = rp.DtTable
            DataGrid2.DataBind()
            DataGrid2.UpdateAfterCallBack = True
        End If

    End Sub
    Function AddNums(ByVal Num1 As Single, ByVal Num2 As Single) As Single
        AddNums = Num1 + Num2
    End Function

    Protected Sub btnGrabaSol_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles btnGrabaSol.Click
        Dim exec As New DotNetDal.SQLPersistence(True, "cnn", DotNetDal.SQLPersistence.Provider.SQLCLIENT)
        Dim rp As New DotNetResponse.SQLPersistence
        Dim cont As Integer = 0

        For i = 0 To DataGrid2.Items.Count - 1
            If DirectCast(DataGrid2.Items(i).Cells(0).FindControl("CheckBox1"), Anthem.CheckBox).Checked = True Then
                cont = cont + 1
                If DirectCast(DataGrid2.Items(i).Cells(7).FindControl("TextBox2"), Anthem.TextBox).Text.ToString = "" Then
                    Mensaje("Wfrm_RecepcionaSolicitud", "Ingr")
                    Exit Sub
                End If
                Dim a As Integer = AddNums(DirectCast(DataGrid2.Items(i).Cells(7).FindControl("TextBox2"), Anthem.TextBox).Text.ToString, DataGrid2.Items(i).Cells(6).Text.ToString.Replace("&nbsp;", 0))
                If a <= DataGrid2.Items(i).Cells(5).Text.ToString() Then
                    rp.ClearParametro()
                    rp.TieneDatos = False
                    rp.StoredProcedure = "SP_OC_INSERT_RECEPCION_COMPRA"
                    rp.AddParametro(DirectCast(DataGrid2.Items(i).Cells(8).FindControl("TextBox1"), Anthem.TextBox).Text.ToString) 'N° DOCUMENTO
                    rp.AddParametro(DirectCast(DataGrid2.Items(i).Cells(9).FindControl("DropDownList1"), Anthem.DropDownList).SelectedItem.Text.ToString) 'TIPO DOCUMENTO
                    rp.AddParametro(DataGrid2.Items(i).Cells(1).Text.ToString) 'NUMERO O.C
                    rp.AddParametro(DataGrid2.Items(i).Cells(2).Text.ToString) 'NUMERO SOLICITUD
                    rp.AddParametro(DataGrid2.Items(i).Cells(10).Text.ToString) 'codigo producto
                    exec.ExecuteNonQuery(rp)

                    rp.ClearParametro()
                    rp.TieneDatos = False
                    rp.StoredProcedure = "SP_OC_UPDATE_OC_SOLICITUD_PRODUCTO"
                    rp.AddParametro(a) 'DirectCast(DataGrid2.Items(i).Cells(7).FindControl("TextBox2"), Anthem.TextBox).Text.ToString)
                    rp.AddParametro(DataGrid2.Items(i).Cells(2).Text.ToString)
                    rp.AddParametro(DataGrid2.Items(i).Cells(10).Text.ToString)
                    exec.ExecuteNonQuery(rp)
                Else
                    Mensaje("Wfrm_RecepcionaSolicitud", "Error")
                    exec.Rollback()
                    Exit Sub
                End If
            End If
        Next
        If cont = 0 Then
            Mensaje("Wfrm_RecepcionaSolicitud", "chk")
            Exit Sub
        Else
            pnl_mensaje.Visible = False
            pnl_mensaje.UpdateAfterCallBack = True
        End If
        exec.Commit()

        If rp.Errores Then
            Mensaje(rp.MensajeError)
        Else
            carga_grilla()
            Mensaje("Recepción Grabada Correctamente")
        End If

    End Sub
    Sub Mensaje(ByVal tbl As String, ByVal codigo As String)
        'pnl_mensaje.Visible = True
        Dim Mensaje As String ' = FuncionesGlobal.GetMensaje(tbl, codigo)

        Mensaje = FuncionesGlobal.GetMensaje(tbl, codigo)


        If Anthem.Manager.IsCallBack Then
            Anthem.Manager.AddScriptForClientSideEval("alert('" & Mensaje & "')")
        Else
            Page.ClientScript.RegisterStartupScript(Me.GetType, "alertar", "<script>alert('" & Mensaje & "')</script>")
        End If


    End Sub
    Sub Mensaje(ByVal Msg As String)

        If Anthem.Manager.IsCallBack Then
            Anthem.Manager.AddScriptForClientSideEval("alert('" & Msg & "')")
        Else
            Page.ClientScript.RegisterStartupScript(Me.GetType, "alertar", "<script>alert('" & Msg & "')</script>")
        End If

    End Sub

   
End Class